package com.dat255.tentatime;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * This Activity handles login/out user
 * @author Daniel Kristoffersson
 * 
 */
public class LoginActivity extends Activity implements OnClickListener {
	// Textbox for Admin
	private EditText editAdmin;
	// Textbox for Password
	private EditText editPass;
	private Button buttonLogin;
	private TextView lblResult;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		// Assign components
		editAdmin = (EditText) findViewById(R.id.etAdmin);
		editPass = (EditText) findViewById(R.id.etPassword);
		buttonLogin = (Button) findViewById(R.id.bLogin);
		lblResult = (TextView) findViewById(R.id.loginResult);

		// Initiate listener
		buttonLogin.setOnClickListener(this);

	}

	/*
	 * This method will run when the user presses the find button
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {

		UserState app = ((UserState)LoginActivity.this.getApplication());


		String username = editAdmin.getText().toString();
		String password = editPass.getText().toString();
		
		// Hard coded username and password
		if (username.equals("guest") && password.equals("guest") ) {

			app.loginUser();

			finish();

		} else {
			lblResult.setText("Login failed. Username and/or password doesn't match.");
			editAdmin.setText("");
			editPass.setText("");
		}

		Log.d("LoginActivity", "onClick'd with" + " " + username + " "
				+ password);

	}

	// /////////// MENU /////////////

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {

		case R.id.itemFind:
			startActivity(new Intent(this, FindActivity.class));
			break;
		}

		return true;
	}
}
